package com.qinghuan.threads.chapterOne;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @Author: dubao
 * @Date: 2022/5/16 19:15
 * @Description:
 */
public class ThreadTest {

    public static class MyThread extends Thread {
        @Override
        public void run() {
            System.out.println("I am a child thread 1");
        }
    }

    public static class RunnableTask implements Runnable {
        @Override
        public void run() {
            System.out.println("I am a chile thread 2");
        }
    }

    public static class CallerTask implements Callable<String> {
        @Override
        public String call() throws Exception {
            return "hello";
        }
    }

    public static void main(String[] args) {

        /**
         * 继承Thread类实现多线程
         */
//        MyThread myThread = new MyThread();
//        myThread.start();

        /**
         * 实现Runnable接口
         */
//        RunnableTask runnableTask = new RunnableTask();
//        new Thread(runnableTask).start();
//        new Thread(runnableTask).start();

        /**
         * Callable实现线程的写法
         */
        //创建异步任务
        FutureTask<String> futureTask = new FutureTask<>(new CallerTask());
        new Thread(futureTask).start();
        try {
            String result = futureTask.get();
            System.out.println(result);
        } catch (ExecutionException | InterruptedException e) {
            e.printStackTrace();
        }
    }

}
